package com.fms.kccc.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

import com.fms.kccc.object.ImageContent;
import com.fms.kccc.service.EMFService;

public class ImageContentDAOImpl implements ImageContentDAO {

	private final String ALL_CATEGORY = "all";

	@Override
	public void add(ImageContent imageContent) {
		// TODO Auto-generated method stub
		synchronized (this) {
			EntityManager em = EMFService.get().createEntityManager();
			em.persist(imageContent);
			em.close();
		}
	}

	@Override
	public List<ImageContent> getImageContents(String category, int page,
			int rows) {
		// Begin page = 1 not page = 0;
		if (page == 0) {
			page = 1;
		}
		int first = (page - 1) * rows;
		int max = rows;
		EntityManager em = EMFService.get().createEntityManager();
		/*
		 * Query q = em .createQuery(
		 * "SELECT c FROM ImageContent c WHERE c.category = :category ORDER BY c.createAt ASC, c.category ASC"
		 * );
		 */
		// q.setParameter("category", category);
		// q.setFirstResult(first);
		// q.setMaxResults(max);

		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<ImageContent> cq = cb.createQuery(ImageContent.class);

		Root<ImageContent> imageContent = cq.from(ImageContent.class);
		cq.select(imageContent);

		ParameterExpression<String> p = cb.parameter(String.class);
		if (!(category == null || category.equals(ALL_CATEGORY))) {
			cq.where(cb.equal(imageContent.get("category"), p));
		}
		cq.orderBy(cb.desc(imageContent.get("createAt")));

		TypedQuery<ImageContent> query = em.createQuery(cq);
		query.setParameter(p, category);
		query.setFirstResult(first);
		query.setMaxResults(max);
		List<ImageContent> results = query.getResultList();
		// Increase counter
		return results;
	}

	@Override
	public List<ImageContent> getMarkedImageContents(String category,
			Date date, int page, int rows) {
		// Begin page = 1 not page = 0;
		if (page == 0) {
			page = 1;
		}
		int first = (page - 1) * rows;
		int max = rows;
		EntityManager em = EMFService.get().createEntityManager();
		/*
		 * Query q = em .createQuery(
		 * "SELECT c FROM ImageContent c WHERE c.category = :category ORDER BY c.createAt ASC, c.category ASC"
		 * );
		 */
		// q.setParameter("category", category);
		// q.setFirstResult(first);
		// q.setMaxResults(max);

		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<ImageContent> cq = cb.createQuery(ImageContent.class);

		Root<ImageContent> imageContent = cq.from(ImageContent.class);
		cq.select(imageContent);

		ParameterExpression<String> p = cb.parameter(String.class);
		if (!(category == null || category.equals(ALL_CATEGORY))) {
			//cb.equal(imageContent.get("category"), p);
		}
		ParameterExpression<Date> pDate = cb.parameter(Date.class);
		cq.where(cb.lessThanOrEqualTo(
				imageContent.<Date> get("createAt"), pDate));
		
		cq.orderBy(cb.desc(imageContent.get("createAt")));

		TypedQuery<ImageContent> query = em.createQuery(cq);
		query.setParameter(p, category);
		query.setParameter(pDate, date);
		query.setFirstResult(first);
		query.setMaxResults(max);
		List<ImageContent> results = query.getResultList();
		// Increase counter
		return results;
	}
}
